home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / T / TIFF Code.cpt / image.c < prev    next >
Text File  |  1987-12-16  |  7KB  |  270 lines

  1. /* Standard C Include files */
  2. /* #include "CType.h" */
  3. /* #include "ErrNo.h" */
  4. /* #include "FCntl.h" */
  5. /* #include "IOCtl.h" */
  6. /* #include "Math.h" */
  7. /* #include "SetJmp.h" */
  8. /* #include "Signal.h" */
  9. /* #include "StdIO.h" */
  10. /* #include "String.h" */
  11.  
  12. /* Primary Interface Files */
  13. #include "Types.h"
  14. #include "Resources.h"
  15. #include "Quickdraw.h"
  16. #include "Windows.h"
  17. /* #include "OSUtils.h" */
  18.  
  19. /* Commonly Included files */
  20. /* #include "ToolUtils.h" */
  21. #include "TextEdit.h"
  22. /* #include "Controls.h" */
  23.  
  24. /* Other Interface files */
  25. /* #include "AppleTalk.h" */
  26. /* #include "CursorCtl.h" */
  27. /* #include "Desk.h" */
  28. /* #include "DeskBus.h" */
  29. /* #include "Devices.h" */
  30. /* #include "Dialogs.h" */
  31. /* #include "DiskInit.h" */
  32. /* #include "Disks.h" */
  33. /* #include "ErrMgr.h" */
  34. /* #include "Errors.h" */
  35. /* #include "Events.h" */
  36. #include "Files.h"
  37. /* #include "Fonts.h" */
  38. /* #include "Graf3D.h" */
  39. /* #include "Lists.h" */
  40. #include "Memory.h"
  41. /* #include "Menus.h" */
  42. /* #include "Packages.h" */
  43. /* #include "Palette.h" */
  44. /* #include "Perf.h" */
  45. /* #include "Picker.h" */
  46. /* #include "Printing.h" */
  47. /* #include "Retrace.h" */
  48. /* #include "ROMDefs.h" */
  49. /* #include "SANE.h" */
  50. #include "Scrap.h"
  51. /* #include "Script.h" */
  52. /* #include "SCSI.h" */
  53. /* #include "SegLoad.h" */
  54. /* #include "Serial.h" */
  55. /* #include "Slots.h" */
  56. /* #include "Sound.h" */
  57. /* #include "Start.h" */
  58. /* #include "Strings.h" */
  59. /* #include "Time.h" */
  60. /* #include "Traps.h" */
  61. /* #include "Values.h" */
  62. /* #include "VarArgs.h" */
  63. /* #include "Video.h" */
  64.  
  65. /* Application-specific Include files */
  66. #include "::TiffLibrary:TIFFLib.h"
  67. #include "sample.h"
  68. #include "messages.h"
  69.  
  70. #define MAXCOPYBYTES (3 * 1024)
  71.  
  72. /*
  73.  * ClipPictToOffScreenImage
  74.  *
  75.  *    get PICT from clipboard and write to an offscreen grafport.
  76.  */
  77. void ClipPictToImage(myBitMapPtr)
  78. BitMap *myBitMapPtr;
  79. {
  80.     BitMap        tempMap;
  81.     GrafPtr        newPort, oldPort;
  82.     Rect        rect;
  83.     Size        mapSize;
  84.     short        rowBytes;
  85.     PicHandle    pictureHandle;
  86.     OSErr        error;
  87.     long        offset;
  88.  
  89.     if (myBitMapPtr->baseAddr == nil)
  90.         MyDisposPtr(&myBitMapPtr->baseAddr);    
  91.     
  92.     /* get picture from clipboard */
  93.     if ((pictureHandle = MyNewHandle(0L)) == nil)    /* get minimum size handle */
  94.         return;
  95.     if (GetScrap(pictureHandle, 'PICT', &offset) < 0) {
  96.         /* notify user that there was nothing to paste */
  97.         ErrorMessage(BADCLIP);
  98.         return;
  99.     }
  100.     
  101.     /* get pictures rect and offset to global coordinates (origin 0,0) */
  102.     rect = (**pictureHandle).picFrame;
  103.     OffsetRect(&rect, -(rect.left), -(rect.top));
  104.  
  105.     /* set rowbytes, and bit map size */
  106.     rowBytes = (((rect.right - rect.left - 1) / (2 * 8)) + 1) * 2;
  107.     mapSize = (rect.bottom - rect.top) * rowBytes;
  108.     
  109.     /* set up a bit map */
  110.     tempMap.bounds = rect;
  111.     tempMap.rowBytes = rowBytes;
  112.     if ((tempMap.baseAddr = MyNewPtr(mapSize)) == nil) {
  113.         /* notify user that there was a memory error */
  114.         DisposHandle(pictureHandle);
  115.         return;
  116.     }
  117.  
  118.     /* set up a new grafPort to use above bitmap */
  119.     if ((newPort = MyNewPtr((Size)sizeof(GrafPort))) == nil) {
  120.         /* notify user that there was a memory error */
  121.         DisposHandle(pictureHandle);
  122.         MyDisposPtr(tempMap.baseAddr);
  123.         return;
  124.     }
  125.     GetPort(&oldPort);                    /* remember the previous port */
  126.     OpenPort(newPort);                    /* initialize newPort */
  127.     SetPort(newPort);                    /* make current port our own */
  128.     SetPortBits(&tempMap);                /* replace screenbitmap */
  129.     PortSize(rect.right, rect.bottom);            /* set newPort's portRect */
  130.     RectRgn(newPort->visRgn, &tempMap.bounds);    /* set visRgn to bitmap's bounds rect */
  131.     
  132.     /* draw clipboard picture into our new port */
  133.     DrawPicture(pictureHandle, &rect);
  134.     DisposHandle(pictureHandle);
  135.     
  136.     /* restore previous port */
  137.     SetPort(oldPort);
  138.     DisposPtr(newPort);
  139.     
  140.     *myBitMapPtr = tempMap;
  141.     DisplayImage(FrontWindow(), myBitMapPtr);
  142.     return;
  143. }
  144.     
  145. void ImageToClipPict(myBitMapPtr)
  146. BitMap    *myBitMapPtr;
  147. {
  148.     PicHandle    pictureHandle;
  149.     GrafPtr        newPort, oldPort;
  150.     Rect        rect, copyRect;
  151.     Size        picSize;
  152.     Int32        maxCopyRows;
  153.     
  154.     /* set up a new grafPort to create a picture with */
  155.     if ((newPort = MyNewPtr((Size)sizeof(GrafPort))) == nil)
  156.         return;
  157.     GetPort(&oldPort);                    /* remember the previous port */
  158.     OpenPort(newPort);                    /* initialize newPort */
  159.     SetPort(newPort);                    /* make current port our own */
  160.     SetPortBits(myBitMapPtr);            /* replace screenbitmap */
  161.     rect = myBitMapPtr->bounds;
  162.     PortSize(rect.right - rect.left, rect.bottom - rect.top);
  163.     RectRgn(newPort->visRgn, &rect);    /* set visRgn to bounds */
  164.  
  165.     /* creat pict of our image */
  166.     pictureHandle = OpenPicture(&rect);
  167.  
  168.     maxCopyRows = MAXCOPYBYTES / myBitMapPtr->rowBytes;
  169.     copyRect = rect;
  170.     while (copyRect.top < rect.bottom) {
  171.         copyRect.bottom = MIN(copyRect.top + maxCopyRows, rect.bottom);
  172.         CopyBits(&newPort->portBits, &newPort->portBits,
  173.                  ©Rect, ©Rect, srcCopy, nil);
  174.         copyRect.top = copyRect.bottom;
  175.     }
  176.     
  177.     ClosePicture();
  178.  
  179.     /* put it in the scrap */
  180.     MoveHHi(pictureHandle);
  181.     HLock(pictureHandle);
  182.     picSize = GetHandleSize(pictureHandle);
  183.     if (ZeroScrap() == noErr)
  184.         if (PutScrap(picSize, 'PICT', &(**pictureHandle)) != noErr)
  185.             ZeroScrap();
  186.     HUnlock(pictureHandle);
  187.     KillPicture(pictureHandle);
  188.     
  189.     /* restore previous port */
  190.     SetPort(oldPort);
  191.     DisposPtr(newPort);
  192. }
  193.  
  194. void DisplayImage(myWindowPtr, myBitMapPtr)
  195. WindowPtr    myWindowPtr;
  196. BitMap        *myBitMapPtr;
  197. {
  198.     Rect    rect,
  199.             srcRect,
  200.             dstRect,
  201.             srcRectStrip,
  202.             dstRectStrip;
  203.     Int32    maxSrcRows,
  204.             maxDstRows,
  205.             srcRows,
  206.             dstRows,
  207.             srcCols,
  208.             dstCols,
  209.             srcPerDst,
  210.             dstPerSrc;
  211.     
  212.     EraseRect(&myWindowPtr->portRect);
  213.     if (myBitMapPtr->baseAddr != nil) {
  214.         /* initialize some variables */
  215.         srcRect = myBitMapPtr->bounds;
  216.         dstRect = (qd.thePort)->portRect;
  217.         srcRows = srcRect.bottom - srcRect.top;
  218.         dstRows = dstRect.bottom - dstRect.top;
  219.         srcCols = srcRect.right - srcRect.left;
  220.         dstCols = dstRect.right - dstRect.left;
  221.         
  222.         /* maximum number of source image rows less than 3K of memory */
  223.         maxSrcRows = MIN(srcRows, (MAXCOPYBYTES / myBitMapPtr->rowBytes));
  224.         
  225.         /* compute ratio of source to destination */
  226.         if (srcRows > dstRows) {
  227.             srcPerDst = (srcRows + (dstRows - 1)) / dstRows;
  228.             maxDstRows = maxSrcRows / srcPerDst;
  229.             dstCols = srcCols / srcPerDst;    /* same proportions */
  230.         }
  231.         else {
  232.             dstPerSrc = dstRows / srcRows;
  233.             maxDstRows = maxSrcRows * dstPerSrc;
  234.             dstCols = srcCols * dstPerSrc;    /* same proportions */
  235.         }
  236.         dstRect.right = dstRect.left + dstCols;
  237.         
  238.         /* copy one strip at a time */
  239.         srcRectStrip = srcRect;
  240.         dstRectStrip = dstRect;
  241.         while (srcRectStrip.top < srcRect.bottom) {
  242.             srcRectStrip.bottom = srcRectStrip.top + maxSrcRows;
  243.             if (srcRectStrip.bottom <= srcRect.bottom)
  244.                 dstRectStrip.bottom = dstRectStrip.top + maxDstRows;
  245.             else {                /* last strip, copy whatever is left */
  246.                 srcRectStrip.bottom = srcRect.bottom;
  247.                 if (srcRows > dstRows)
  248.                     dstRectStrip.bottom =
  249.                         (srcRectStrip.bottom - srcRectStrip.top) / srcPerDst;
  250.                 else
  251.                     dstRectStrip.bottom =
  252.                         (srcRectStrip.bottom - srcRectStrip.top) * dstPerSrc;
  253.             }
  254.             CopyBits(myBitMapPtr, &(qd.thePort)->portBits,
  255.                      &srcRectStrip, &dstRectStrip, srcCopy, nil);
  256.             srcRectStrip.top = srcRectStrip.bottom;
  257.             dstRectStrip.top = dstRectStrip.bottom;
  258.         }
  259.     }
  260. }
  261.  
  262. void ClearImage(myBitMapPtr)
  263. BitMap    *myBitMapPtr;
  264. {
  265.     WindowPtr theActiveWindow;
  266.         
  267.     theActiveWindow = FrontWindow();
  268.     EraseRect(&theActiveWindow->portRect);
  269.     MyDisposPtr(&myBitMapPtr->baseAddr);
  270. }